<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Sergey
 * Date: 08.08.13
 * Time: 12:33
 * To change this template use File | Settings | File Templates.
 */

namespace Hockey\Controller;

//echo '<pre>';
//print_r(BASE_PATH);
//echo '</pre>';

use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Hockey\Dao\Doctrine\DbConnection;
use Hockey\Persistence\Order;

class IndexController
{
    public function __construct()
    {
    }

    public function index(Application $app)
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_index.twig')
        );

        return $response;
    }

    public function about(Application $app)
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_about.twig')
        );

        return $response;
    }

    public function gallery(Application $app)
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_gallery.twig')
        );

        return $response;
    }

    public function contacts(Application $app)
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_contacts.twig')
        );

        return $response;
    }

    public function checkout_get(Application $app)
    {
        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_checkout_get.twig')
        );

        return $response;
    }

    public function checkout_post(Application $app, Request $request)
    {
        $order = new Order();
        $order->setName(trim(strip_tags($request->request->get('name'))));
        $order->setSurname(trim(strip_tags($request->request->get('surname'))));
        $order->setPhone(trim(strip_tags($request->request->get('phone'))));
        $order->setEmail(trim(strip_tags($request->request->get('email'))));
        $order->setCity(trim(strip_tags($request->request->get('city'))));
        $order->setAdditional(trim(strip_tags($request->request->get('additional'))));

        DbConnection::getInstance()->getEntityManager()->persist($order);
        DbConnection::getInstance()->getEntityManager()->flush();

        $response = new Response();
        $response->headers->set('Content-Type', 'text/html');
        $response->setContent(
            $app['twig']->render('layout_checkout_post.twig')
        );

        return $response;
    }
}